﻿#region Copyright & License

// Copyright © 2012 - 2013 François Chabot, Yves Dierick
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System.IO;
using Be.Stateless.IO.Extensions;
using Be.Stateless.Linq.Extensions;

namespace Be.Stateless.BizTalk
{
	internal static class DropFolders
	{
		public static void ClearInputFolders()
		{
			Directory.GetFiles(INPUT_FOLDER).Each(File.Delete);
		}

		public static void ClearOutputFolders()
		{
			Directory.GetFiles(CHECK_IN_FOLDER).Each(File.Delete);
			Directory.GetFiles(CHECK_OUT_FOLDER).Each(File.Delete);
			Directory.GetDirectories(CHECK_OUT_FOLDER).Each(d => Directory.Delete(d, true));
			Directory.GetFiles(OUTPUT_FOLDER).Each(File.Delete);
			Directory.GetFiles(TRACE_FOLDER).Each(File.Delete);
		}

		internal static void SaveToCheckOutFolder(this Stream stream, string name)
		{
			stream.SaveToPath(Path.Combine(CHECK_OUT_FOLDER, name));
		}

		internal static void SaveToInputFolder(this Stream stream, string name)
		{
			stream.SaveToPath(Path.Combine(INPUT_FOLDER, name));
		}

		private static void SaveToPath(this Stream stream, string path)
		{
			File.Delete(path);
			stream.Save(path + ".tmp");
			File.Move(path + ".tmp", path);
			stream.Close();
		}

		internal const string CHECK_IN_FOLDER = @"C:\Files\Drops\Tracking";
		internal const string CHECK_OUT_FOLDER = @"C:\Files\Drops\Claims";
		internal const string INPUT_FOLDER = @"C:\Files\Drops\In";
		internal const string OUTPUT_FOLDER = @"C:\Files\Drops\Out";
		internal const string TRACE_FOLDER = @"C:\Files\Drops\Trace";
	}
}
